package at.fhooe.mc.master.clc.client.view;

import at.fhooe.mc.master.clc.client.presenter.MetricsRequestPresenter.IMetricsRequestView;
import at.fhooe.mc.master.clc.client.presenter.MetricsVisualizationPresenter.IMetricsVisualizationView;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.event.logical.shared.OpenEvent;
import com.google.gwt.event.logical.shared.OpenHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class MetricsVisualizationView extends Composite implements IMetricsVisualizationView
{
public final static int REQUEST_HEIGHT_OPEN = 380;
	
	private DockLayoutPanel dockLayoutPanel = new DockLayoutPanel(Unit.PX);
	
	private Label label = new Label("Test label displayed using MVP");
	
	private VerticalPanel graphPanel = new VerticalPanel();
	private ScrollPanel panel = new ScrollPanel(graphPanel);
	
	private IsWidget requestView;
	
	public MetricsVisualizationView()
	{
		initWidget(dockLayoutPanel);
	}
	
	@Override
	public void setDataRequestWidget(IsWidget dataRequestWidget)
	{
		this.requestView = dataRequestWidget;
		
		dockLayoutPanel.addNorth(dataRequestWidget, REQUEST_HEIGHT_OPEN);
		dockLayoutPanel.add(panel);
		
		if (dataRequestWidget instanceof MetricsRequestView)
		{
			DisclosurePanel dp = ((MetricsRequestView) dataRequestWidget).getCollapsiblePanel();
			dp.addOpenHandler(new OpenHandler<DisclosurePanel>()
			{
				
				@Override
				public void onOpen(OpenEvent<DisclosurePanel> event)
				{
					dockLayoutPanel.setWidgetSize(requestView.asWidget(), MetricsVisualizationView.REQUEST_HEIGHT_OPEN);
				}
			});
			
			dp.addCloseHandler(new CloseHandler<DisclosurePanel>()
			{
				
				@Override
				public void onClose(CloseEvent<DisclosurePanel> event)
				{
					dockLayoutPanel.setWidgetSize(requestView.asWidget(), 30);
				}
			});
		}
	}
	
//	@Override
//	public void setGraphWidget(IsWidget graph)
//	{
//		graphPanel.setSize(Window.getClientWidth() - 25 + "px", Window.getClientHeight() - requestWidgetHeight + "px");
//		graphPanel.add(graph);
//	}
	
	@Override
	public Widget asWidget()
	{
		return this;
	}

	@Override
	public void addGrapWidget(IsWidget graph, int key)
	{
		System.out.println("Add graph!");
		
		graphPanel.add(graph);
	}

	@Override
	public void removeGraphWidget(IsWidget graph, int key)
	{
		int indexToRemove = -1;
		for (int i = 0; i < graphPanel.getWidgetCount(); i++)
		{
			if (graphPanel.getWidget(i) instanceof IAbstractGraphView)
			{
				IAbstractGraphView v = (IAbstractGraphView)graphPanel.getWidget(i);
				if (v.getKey() == key)
				{
					indexToRemove = i;
					break;
				}
			}
		}
		
		if (indexToRemove != -1)
		{
			graphPanel.remove(indexToRemove);
		}
	}
}
